/**
 * Project: springyounger.lottery.web
 * 
 * File Created at 2010-3-21
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyouner.lottery.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.util.WebUtils;

import com.springyouner.lottery.framework.UserForm;
import com.springyouner.lottery.framework.UserSession;
import com.springyouner.lottery.tools.Encrypt;
import com.springyouner.lottery.tools.LotteryUtil;
import com.springyounger.lottery.dal.dataobject.User;
import com.springyounger.lottery.service.user.UserService;

/**
 * 用户注册修改
 * 
 * @author springyounger
 */
public class UserFormController extends SimpleFormController {
	private static final Logger logger = LoggerFactory
			.getLogger(UserFormController.class);
	private UserService userService;

	public UserFormController() {
		// setSessionForm(true);
		// setValidateOnBinding(false);
		setCommandClass(UserForm.class);
		setCommandName("userForm");
		setFormView("reg");
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		UserSession userSession = (UserSession) WebUtils.getSessionAttribute(
				request, "userSession");
		if (userSession != null) {
			return new UserForm(userService.getUser(userSession.getUser()
					.getUsername()));
		} else {
			return new UserForm();
		}
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		UserForm userForm = (UserForm) command;
		User user = userForm.getUser();
		// 去除空格
		user.setUsername(LotteryUtil.trim(user.getUsername()));
		user.setPassword(LotteryUtil.trim(user.getPassword()));
		user.setEmail(LotteryUtil.trim(user.getEmail()));
		if (userForm.isNewUser()) {
			// 检查用户名是否重复
			if ((userService.getUser(user.getUsername()) != null)) {
				logger.info("用户名重复 {}", user.getUsername());
				errors.rejectValue("user.username", "error.username.dup",
						"用户名重复");
				return showForm(request, response, errors);
			} else {
				// 加密
				logger.info("创建用户 {}", user.getUsername());
				user.setPassword(Encrypt.encodeMD5(user.getPassword()));
				user.setRoleid(String.valueOf(User.PRIVILEGE_USER));
				userService.insertUser(user);
			}
		} else {
			user.setPassword(Encrypt.encodeMD5(user.getPassword()));
			logger.info("更新用户 {} ", user.getUsername());
			userService.updateUser(user);
		}

		return super.onSubmit(request, response, command, errors);
	}

	/**
	 * @param userService
	 *            the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
